package iputils

import "net"

func ByteOrderNtoH(s string) string {

	for i := 0; i < len(s); i++ {
		switch s[i] {
		case '.':
			return byteOrderIPv4NtoH(s)
		case ':':
			return byteOrderIPv6NtoH(s)
		}
	}
	return ""
}

func byteOrderIPv6NtoH(s string) string {
	ip := net.ParseIP(s).To16()

	// check ip boundary
	_ = ip[15]

	for i := 0; i < 4; i++ {
		head := i * 4
		ip[head], ip[head+3] = ip[head+3], ip[head]
		ip[head+1], ip[head+2] = ip[head+2], ip[head+1]
	}

	return ip.String()
}

func byteOrderIPv4NtoH(s string) string {
	ip := net.ParseIP(s).To16()

	// check ip boundary
	_ = ip[15]

	ip[12], ip[15] = ip[15], ip[12]
	ip[13], ip[14] = ip[14], ip[13]
	return ip.String()
}
